import 'package:courseapp/entity/stusigndto.dart';
import 'package:courseapp/http/HttpUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class SignRecordWidget extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return SignRecordState();
  }

}

class SignRecordState extends State<StatefulWidget> {

  int page = 1;
  bool isLoading = false;
  List<DataBean> _list = [];
  ScrollController scrollController = new ScrollController();

  @override
  void initState() {
    // TODO: implement initState
    scrollController.addListener((){
     var scrollPosition = scrollController.position;
     if(scrollPosition.maxScrollExtent - scrollPosition.pixels < 50){
       if(!isLoading){
         isLoading = true;
         setState((){});
         print("加载数据......");
         loadData();
        }
     }
    });
    initData();
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: RefreshIndicator(
        backgroundColor: Colors.white,
        onRefresh: initData,
        child: ListView.builder(
            controller: scrollController,
            itemBuilder: _createItem,
            itemCount: _list.length == 0 ? 0: isLoading?_list.length + 1 : _list.length,
        ),
      ),
    );
  }


  @override
  void dispose() {
    scrollController?.dispose();
    super.dispose();
  }

  Widget _createItem(BuildContext context,int index) {
    if(index < _list.length) {
      return generateNormal(_list[index]);
    }
      return generateProgress();
  }

  Widget generateNormal(DataBean item) {
     String courseName = item.courseName;
     String date = item.signDay;
     String state = item.isSign ? "已签到":"未签到";
      return Container(padding: EdgeInsets.all(5),alignment: Alignment.centerLeft,
      decoration: BoxDecoration(border: Border(bottom : Divider.createBorderSide(context, color: Colors.grey))),
      child: Column(children: <Widget>[
        Container(alignment: Alignment.centerLeft,child:Text("签到课程: $courseName")),
        Container(alignment: Alignment.centerLeft,child:Text("签到日期: $date")),
        Container(alignment: Alignment.centerLeft,child:Text("签到状态: $state")),
      ],),
      );
  }

  Widget generateProgress() {
    return Center(
      child: Padding(
        padding: EdgeInsets.all(10.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            SizedBox(
              width: 20,
              height: 20,
              child: CircularProgressIndicator(
                strokeWidth: 4.0,
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(vertical: 5, horizontal: 15),
              child: Text(
                '加载中...',
                style: TextStyle(fontSize: 16.0),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future<void>loadData() async {
     page++;
     new Future.delayed(Duration(seconds: 3),() async {
       Stusigndto stuSignDto =  await  HttpUtil.getInstance().postStuSign(page);
       _list.addAll(stuSignDto.data);
       isLoading = false;
       setState(() {});
     });
  }

  Future<void> initData() async {
   Stusigndto stusigndto =  await  HttpUtil.getInstance().postStuSign(page);
   _list.addAll(stusigndto.data);
   setState(() {});
  }



}

